<?php
class Func{
    public static function dispatch_action(){
        $action = rtrim(@$_GET['action'], '/');
        $action_func = "action_{$action}";
        is_callable($action_func) ? $action_func() : action_index();
    }
    
    public static function generate_salt(){
        $randNum = rand(0,65535);
        $md5Str = md5($randNum);
        return substr($md5Str,0,6);
    }
    public static function get_datetime($time=''){
        $fmt = 'Y-m-d H:i:s';
        if(empty($time)){
            return CTimestamp::formatDate($fmt);
        }
        if(is_integer($time)){
            return CTimestamp::formatDate($fmt,$time);
        }
        return '';
    }
    public static function get_ip($type=''){
        if(empty($type)){
            $type = 'int';
        }
        if($type == 'int'){
            return ip2long( Yii::app()->request->getUserHostAddress() );
        }
    }
    
    public static function get_deleteajax(){
        $csrfTokenName = Yii::app()->request->csrfTokenName;
        $csrfToken = Yii::app()->request->csrfToken;
        $csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
        $Confirmation= "你确定要这么做？";
        $afterDelete = 'function(link,success,data){ if(success) alert(data); }';
        $click=<<<EOD
        function() {
        if(!confirm("$Confirmation")) return false;;
        var th=this;
        var afterDelete=$afterDelete;
        var gridview_id = $(".grid-view").attr("id");
        $.fn.yiiGridView.update(gridview_id, {
        type:'POST',
        url:$(this).attr('href'),$csrf
        success:function(data) {
        $.fn.yiiGridView.update(gridview_id);
            afterDelete(th,true,data);
        },
        error:function(XHR) {
            return afterDelete(th,false,XHR);
        }
        });
        return false;
        }
EOD;
    
        return $click;
    }
    
    public static function generate_modulecode($type='default',$sign='0'){
        return $type.'_'.$sign.'_'.time();
    }
}